﻿/**************************************************************************
 * FileName：CardVerificationService
 * Function：银行账号验证接口服务类
 * Description：
 * Version：
 * ------------------------------------------
 * CreateTime：2024/5/21 10:26
 * Author：金文旭
 * Email： ohayashi@163.com
 * =============================================
 * 
 * Modifyier：	
 * ModifyTime：	
 * Description：	
 ***************************************************************************/
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using static System.Net.WebRequestMethods;

namespace DesignPractise.CardCheck
{
    public class CardVerificationService : ICardVerificationInterface
    {
        public static Lazy<CardVerificationService> Instance=new Lazy<CardVerificationService>();
        private const byte cardMinLength = 14;
        private const byte cardMaxLength = 20;
        private const byte phoneLength = 11;
        private const byte cnameMaxLength = 32;
        private const byte certifIdLength = 18;
        private const string veriUri = @"https://auth.95516.com/authonl/onlineAuth/rest/verify/bankcard";

        private string?appId;
        private string?appPwd;
        private CardVerificationService(string appId, string appPwd)
        {
            this.appId = appId;
            this.appPwd = appPwd;
        }
        private CardVerificationService()
        {
            appId = null;
            appPwd = null;
        }
        public CardVeriMessage ExcuteCardVerification(string cardNo, string customerNm, string phoneNo, string certifId, string app_Id, string app_Pwd, string certifTp, string transCode)
        {
            //用户信息检查
            char[] check;
            check=cardNo.ToCharArray();
            if (check.Length < cardMinLength || check.Length > cardMaxLength) { throw new CardVeriExeption(ResponseCodeEnum.银行卡号格式不合法.ToString()); }
            check=customerNm.ToCharArray();
            if (check.Length > cnameMaxLength) { throw new CardVeriExeption(ResponseCodeEnum.姓名格式不合法.ToString()); }
            check=phoneNo.ToCharArray();
            if (check.Length != phoneLength) { throw new CardVeriExeption(ResponseCodeEnum.手机号格式不合法.ToString()); }
            check = certifId.ToCharArray();
            if (check.Length != certifIdLength) { throw new CardVeriExeption(ResponseCodeEnum.证件号格式不合法.ToString()); }

            CardPostUnit cardPostUnit=new CardPostUnit(app_Id, app_Pwd,cardNo,customerNm,phoneNo,certifId,certifTp,transCode);
            string json = cardPostUnit.ToString();
            string resp = DoPost(json, veriUri).Result;


            return new CardVeriMessage(resp);

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="cardNo"></param>
        /// <param name="customerNm"></param>
        /// <param name="phoneNo"></param>
        /// <param name="certifId"></param>
        /// <param name="certifTp"></param>
        /// <param name="transCode"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public CardVeriMessage ExcuteCardVerification(string cardNo, string customerNm, string phoneNo, string certifId, string? certifTp = null, string? transCode = null)
        {
            //商户信息检查
            if (appId == null || appPwd == null) { throw new CardVeriExeption(ResponseCodeEnum.商户ID或密码未提供.ToString()); }
            //用户信息检查
            char[] check;
            check = cardNo.ToCharArray();
            if (check.Length < cardMinLength || check.Length > cardMaxLength) { throw new CardVeriExeption(ResponseCodeEnum.银行卡号格式不合法.ToString()); }
            check = customerNm.ToCharArray();
            if (check.Length > cnameMaxLength) { throw new CardVeriExeption(ResponseCodeEnum.姓名格式不合法.ToString()); }
            check = phoneNo.ToCharArray();
            if (check.Length != phoneLength) { throw new CardVeriExeption(ResponseCodeEnum.手机号格式不合法.ToString()); }
            check = certifId.ToCharArray();
            if (check.Length != certifIdLength) { throw new CardVeriExeption(ResponseCodeEnum.证件号格式不合法.ToString()); }

            certifTp ??=CardPostUnit.default_certifTp;
            transCode??=CardPostUnit.default_transCode;
            
            CardPostUnit cardPostUnit=new CardPostUnit(appId,appPwd,cardNo, customerNm, phoneNo, certifId, certifTp, transCode);
            string json = cardPostUnit.ToString();
            string resp = DoPost(json, veriUri).Result;

            return new CardVeriMessage(resp);
        }

        public bool InitAppData(string appId, string appPwd)
        {
            this.appId = appId;
            this.appPwd = appPwd;
            return true;
        }
        /// <summary>
        /// 解析Post返回的Json字符串
        /// </summary>
        /// <param name="resp"></param>
        /// <returns>返回该Json对应的枚举结果</returns>
        private async Task<string> DoPost(string json, string url)
        {
            using(HttpClient client = new HttpClient())
            {
                var content=new StringContent("json",Encoding.UTF8,"application/json");
                HttpResponseMessage response=await client.PostAsync(url,content);
                string result=await response.Content.ReadAsStringAsync();
                return result;
            }
        }
    }
}
